package router

import (
	"ecom-order-sys/handler"
	"github.com/gin-gonic/gin"
	"net/http"
)

func Init() *gin.Engine {

	r := gin.Default()
	r.Use(Cors())
	r.Static("/static", "./static")

	//登录
	r.POST("/deng", handler.Deng)
	//对角色信息进行权限分配处理，完成角色与权限关联
	r.POST("/rolecreate", handler.RoleCreate)
	//借助用户和角色的关联信息，查询用户自身的角色
	r.POST("/rolefind", handler.RoleFind)
	//借助角色和权限关联信息，查询角色的权限信息
	r.POST("/nodefind", handler.NodeFind)
	//实现售后商品信息添加功能，为后续售后订单提供基础
	r.POST("/postcreate", handler.PostCreate)
	//实现售后订单添加功能，建立订单商品绑定
	r.POST("/asscreate", handler.AssCreate)
	//实现订单列表数据功能，整合订单商品等数据信息
	r.POST("/list", handler.List)

	return r
}
func Cors() gin.HandlerFunc {

	return func(c *gin.Context) {
		method := c.Request.Method
		origin := c.Request.Header.Get("Origin")
		if origin != "" {
			c.Header("Access-Control-Allow-Origin", "*")
			c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
			c.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization")
			c.Header("Access-Control-Allow-Credentials", "true")
			c.Set("content-type", "application/json")
		}
		//放行所有OPTIONS方法
		if method == "OPTIONS" {
			c.AbortWithStatus(http.StatusNoContent)
		}
		c.Next()
	}
}
